/*
 * Copyright (c) 2020-2021. the original authors and DEPSEA.ORG
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.depsea.oss.config;

import org.depsea.oss.client.*;
import org.depsea.oss.config.properties.OssProperties;
import org.depsea.oss.strategy.FilenameHandleStrategy;
import org.depsea.oss.strategy.RandomFilenameHandleStrategyImpl;
import org.springframework.beans.BeansException;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Primary;
import org.springframework.lang.NonNull;

import java.lang.reflect.Proxy;

/**
 * 自动装配
 *
 * @author Wang Chengwei
 * @since 1.0.0 2021/11/30 15:37
 */
@Configuration
@EnableConfigurationProperties({
        OssProperties.class,
})
@Import({MinioOssConfiguration.class, AliyunOssConfiguration.class})
@AutoConfigureAfter({MinioOssConfiguration.class, AliyunOssConfiguration.class})
public class OssAutoConfiguration implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(@NonNull ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @Bean
    @ConditionalOnMissingBean(FilenameHandleStrategy.class)
    public FilenameHandleStrategy filenameHandleStrategy() {
        return new RandomFilenameHandleStrategyImpl();
    }

    @Bean
    @Primary
    @ConditionalOnBean(OssTemplate.class)
    public OssTemplate primaryOssTemplate(OssProperties ossProperties) {
        OssType ossType = ossProperties.getPrimaryType();

        OssTemplate ossTemplate = this.applicationContext.getBean(ossType.getOssTemplateClass());
        OssTemplateHandler ossTemplateHandler = new OssTemplateHandler(ossTemplate);

        return (OssTemplate)Proxy.newProxyInstance(this.getClass().getClassLoader(),
                ossTemplate.getClass().getInterfaces(), ossTemplateHandler);
    }

    @Bean
    @Primary
    @ConditionalOnBean(CustomerBucketOssTemplate.class)
    public CustomerBucketOssTemplate primaryCustomerBucketOssTemplate(OssProperties ossProperties) {
        OssType ossType = ossProperties.getPrimaryType();

        CustomerBucketOssTemplate ossTemplate = this.applicationContext.getBean(ossType.getCustomerBucketOssTemplateClass());
        CustomerBucketOssTemplateHandler ossTemplateHandler = new CustomerBucketOssTemplateHandler(ossTemplate);

        return (CustomerBucketOssTemplate)Proxy.newProxyInstance(this.getClass().getClassLoader(),
                ossTemplate.getClass().getInterfaces(), ossTemplateHandler);
    }

    @Bean
    @Primary
    @ConditionalOnBean(PresignedUrlGenerator.class)
    public PresignedUrlGenerator primaryPresignedUrlGenerator(OssProperties ossProperties) {
        OssType ossType = ossProperties.getPrimaryType();

        PresignedUrlGenerator generator = this.applicationContext.getBean(ossType.getPresignedUrlGeneratorClass());
        PresignedUrlGeneratorHandler generatorHandler = new PresignedUrlGeneratorHandler(generator);

        return (PresignedUrlGenerator)Proxy.newProxyInstance(this.getClass().getClassLoader(),
                generator.getClass().getInterfaces(), generatorHandler);
    }
}
